package app.auto.designer.expression;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import app.auto.designer.R;
import app.auto.designer.ActivityBase;
import app.auto.designer.func.FuncViewActivity;
import app.auto.runner.base.MapViewConf;
import app.auto.runner.base.Util;
import app.auto.runner.base.action.ParamType;

public class ExpActivity extends ActivityBase {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_expression);
        List<String> funlist = Util.getClassName(this, "app.auto.runner.func", new ArrayList<String>(), false);
        String pkg = "app.auto.runner.func";

        List ms = new ArrayList();
        for (String f : funlist) {
        Map<String, String> methodstring = new TreeMap<String, String>();

            String func = pkg + "." + f;
            try {
                Class clz = Class.forName(func);
                ParamType pt = (ParamType) clz.getAnnotation(ParamType.class);
                pt.desc();
                pt.type();
                methodstring.put("desc", pt.desc());
                methodstring.put("type", pt.type().toString());

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            methodstring.put("name", f);
            ms.add(methodstring);
        }


        MapViewConf mvf = MapViewConf.with(this).pair("desc->desc").source(R.layout.auto_string_left_right);
        MapViewConf.with(this).conf(mvf).source(ms, getView(R.id.list)).toView();
        ((ListView) getView(R.id.list)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map map = (Map) parent.getItemAtPosition(position);
                startActivityForResult(new Intent(view.getContext(), FuncViewActivity.class).putExtra("data", (Serializable) map), 0);
            }
        });

    }


    //参数模板
    //数据本地
    //
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {

        } else {
            String status = data.getStringExtra("status");
            if (status != null && status.equals("ok")) {
                finish();
            }
        }
    }
}
